package cn.com.mma.mobile.tracking.api;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import cn.com.mma.mobile.tracking.util.LocationUtil;
import cn.com.mma.mobile.tracking.util.Logger;
import cn.com.mma.mobile.tracking.util.SdkConfigUpdateUtil;

/**
 * API入口类
 * 
 * @author lincoln
 * 
 */
public class Countly {
	private static Countly mCountly = new Countly();
	private static Timer nromalTimer, failedTimer;
	private SendEventMessage sendEventMessage;
	private RecordEventMessage recordEventMessage;

	public static Countly sharedInstance() {
		return mCountly;
	}

	/**
	 * 公有方法，暴漏给用户的方法 Begin
	 */
	/**
	 * expose event
	 * 
	 * @param url
	 */
	public void onExpose(String url) {
		try {
			if (recordEventMessage != null)
				recordEventMessage.recordEventWithUrl(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * click event
	 * 
	 * @param url
	 */
	public void onClick(String url) {
		try {
			if (recordEventMessage != null)
				recordEventMessage.recordEventWithUrl(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * clean the queen,shut down the thread
	 */
	public void clearAll() {
		try {
			if (sendEventMessage != null)
				sendEventMessage.clearAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * clean the error queen,shut down the thread
	 */
	public void clearErrorList() {
		try {
			if (sendEventMessage != null)
				sendEventMessage.clearErrorList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 设置是否打开log输出
	 */
	public void setLogState(boolean isPrintOut) {
		Logger.DEBUG_LOG = isPrintOut;
	}

	/**
	 * 初始化SDK
	 * 
	 * @param context
	 */
	public void init(final Context context, final String configUrl) {
		try {
			sendEventMessage = SendEventMessage.getSendEventMessage(context);
			recordEventMessage = new RecordEventMessage(context);
			nromalTimer = new Timer();
			failedTimer = new Timer();
			try {
				nromalTimer.schedule(new TimerTask() {
					@Override
					public void run() {
						sendNormalMessage();
					}
				}, 10 * 1000, Global.NORMAL_MESSAGE_DEFAULT_PEROID);

				failedTimer.schedule(new TimerTask() {
					@Override
					public void run() {
						sendFailedMessage();
					}
				}, 10 * 1000, Global.FAILED_MESSAGE_DEFAULT_PEROID);
			} catch (Exception e) {
				e.printStackTrace();
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						SdkConfigUpdateUtil.initSdkConfigResult(context, configUrl);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送普通文件里的消息
	 */
	public void sendNormalMessage() {
		try {
			if (sendEventMessage != null)
				sendEventMessage.sendNromalList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送错误文件里的消息
	 */
	public void sendFailedMessage() {
		try {
			if (sendEventMessage != null)
				sendEventMessage.sendFailedList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * stop timer
	 */
	public void stopLoopTimer() {
		try {
			if (this.nromalTimer != null) {
				this.nromalTimer.cancel();
				this.nromalTimer.purge();
				this.nromalTimer = null;
			}
			if (this.failedTimer != null) {
				this.failedTimer.cancel();
				this.failedTimer.purge();
				this.failedTimer = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * call when exit the app , shut down the thread
	 */
	public void stopCurrentSendThread() {
		try {
			if (sendEventMessage != null)
				sendEventMessage.stopThread();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 停止位置监听
	 * 
	 * @param context
	 */
	public void stopListenerLocation(Context context) {
		try {
			LocationUtil.getInstance(context).stopListenLocation();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 公有方法，暴漏给用户的方法 End
	 */

}
